//
//  LeeOrderContenViewController.m
//  firstpoint
//
//  Created by 李雪虎 on 2017/7/6.
//  Copyright © 2017年 xzcysoft. All rights reserved.
//

#import "LeeOrderContenViewController.h"
#import "MyOrderTableViewCell.h"
#import "ConfirmOrderViewController.h"//订单详情
@interface LeeOrderContenViewController ()<UITableViewDelegate,UITableViewDataSource>

{
    int page;
}
@property(nonatomic,strong)NSMutableArray *bigArr;
@property(nonatomic,strong)UITableView *myOrderTabelView;//表
@end

@implementation LeeOrderContenViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    page=0;
    _bigArr = [NSMutableArray array];
    self.view.backgroundColor = [UIColor whiteColor];
//    [self addShoppingMallTableView];//添加表
    [self httpURLRquest];
    // Do any additional setup after loading the view.
}
//添加表视图
-(void)addShoppingMallTableView{
    //表格
    _myOrderTabelView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, width_screen, height_screen-64) style:UITableViewStyleGrouped];
    _myOrderTabelView.backgroundColor = [UIColor clearColor];
    _myOrderTabelView.showsVerticalScrollIndicator = NO;//隐藏滑动条
    //    _tabelView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _myOrderTabelView.separatorColor = COLOR(229, 230, 231, 1);//分割线颜色
    _myOrderTabelView.delegate = self;
    _myOrderTabelView.dataSource = self;
    [_myOrderTabelView registerNib:[UINib nibWithNibName:@"MyOrderTableViewCell" bundle:nil] forCellReuseIdentifier:@"OenCell"];

    [self.view addSubview:_myOrderTabelView];
    
}
#pragma mark --tableView 代理方法
//区头高度
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.01;
}
//cell高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 240;
}

//多少行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _bigArr.count;
}
//几个区
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
//区尾高度
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 40;
}

//cell内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    MyOrderTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"OenCell"];
    if (_bigArr.count>0) {
        //订单号
        cell.danHao.text = [NSString stringWithFormat:@"%@",_bigArr[indexPath.row][@"orderCode"]];
        //标题
        cell.titleLable.text = [NSString stringWithFormat:@"%@",_bigArr[indexPath.row][@"goodName"]];
        //金币
        cell.jinBi.text = [NSString stringWithFormat:@"%@ 金币",_bigArr[indexPath.row][@"amount"]];
        //数量
        cell.shuLiabg.text = [NSString stringWithFormat:@"X %@",_bigArr[indexPath.row][@"buyCount"]];
        //下单时间
        cell.xiaDanTime.text = [NSString stringWithFormat:@"%@",_bigArr[indexPath.row][@"createdDate"]];
        //规格
//        NSArray *arr =_bigArr[indexPath.row][@"props"];
        NSLog(@"%@",_bigArr[indexPath.row][@"props"]);
//        NSLog(@"%@",_bigArr[indexPath.row][@"props"][0][@"key"]);
        
        if ([_bigArr[indexPath.row][@"props"] isEqualToString:@"0"]) {
            
        }else{
           
           NSString * string1=[_bigArr[indexPath.row][@"props"] stringByReplacingOccurrencesOfString:@"key="withString:@"\"key\":\""];
            
            NSString * string2 = [string1 stringByReplacingOccurrencesOfString:@", value="withString:@"\",\"value\":\""];
            NSString * string3 = [string2 stringByReplacingOccurrencesOfString:@"}"withString:@"\"}"];
            
            
            NSData *jsonData = [string3 dataUsingEncoding:NSUTF8StringEncoding];
            
            NSError *err;
            
            NSArray *array = [NSJSONSerialization JSONObjectWithData:jsonData
                                                                options:NSJSONReadingMutableContainers
                                                                  error:&err];
            
            
            
            
            
            cell.guiGe.text = [NSString stringWithFormat:@"%@ : %@",array[0][@"key"],array[0][@"value"]];
            
            if (array.count>1) {
                 cell.yanSe.text = [NSString stringWithFormat:@"%@ : %@",array[1][@"key"],array[1][@"value"]];
            }
            
            
        }
        
        
               //状态
        
        if ([_bigArr[indexPath.row][@"status"]intValue]==1009||[_bigArr[indexPath.row][@"status"]intValue]==1006) {
            //待付款
            cell.zhuangTai.text = @"待付款";
            [cell.quXiaoBtn setTitle:@"取消订单" forState:UIControlStateNormal];
            [cell.zhifuBtn setTitle:@"支付订单" forState:UIControlStateNormal];
        }
        if ([_bigArr[indexPath.row][@"status"]intValue]==1003||[_bigArr[indexPath.row][@"status"]intValue]==1008) {
            //代发货
            cell.zhuangTai.text = @"待发货";
            cell.quXiaoBtn.hidden=YES;
            cell.quXiaoBtn.alpha=0;
            [cell.zhifuBtn setTitle:@"取消订单" forState:UIControlStateNormal];
        }
        if ([_bigArr[indexPath.row][@"status"]intValue]==1016) {
            //代收货
            cell.zhuangTai.text = @"待收货";
            cell.quXiaoBtn.hidden=YES;
            cell.quXiaoBtn.alpha=0;
            [cell.zhifuBtn setTitle:@"取消订单" forState:UIControlStateNormal];
        }
        if ([_bigArr[indexPath.row][@"status"]intValue]==1004) {
            //已完成
            cell.zhuangTai.text = @"订单完成";
            cell.quXiaoBtn.hidden=YES;
            cell.quXiaoBtn.alpha=0;
             [cell.zhifuBtn setTitle:@"申请售后" forState:UIControlStateNormal];
        }
        if ([_bigArr[indexPath.row][@"status"]intValue]==1005) {
            cell.quXiaoBtn.hidden=YES;
            cell.quXiaoBtn.alpha=0;
            cell.zhuangTai.text = @"已取消";
            [cell.zhifuBtn setTitle:@"已取消" forState:UIControlStateNormal];
        }
        cell.quXiaoBtn.tag = indexPath.row+10000;
        cell.zhifuBtn.tag = indexPath.row+100;
        [cell.zhifuBtn addTarget:self action:@selector(zhifuButtonClcike:) forControlEvents:UIControlEventTouchUpInside];
        [cell.quXiaoBtn addTarget:self action:@selector(quxiaobtnClicked:) forControlEvents:UIControlEventTouchUpInside];
    }
    
    return cell;
    
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
   
}
-(BOOL)judgeIsEmptyWithString:(NSString *)string
{
    if ([string isEqual:[NSNull null]]||string.length == 0 || [string isEqualToString:@""] || string == nil || string == NULL )
    {
        return YES;
    }
    return NO;
}
//cell 分割线 两端封头 实现这个两个方法 1
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }
    
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
}
//取消订单
-(void)quxiaobtnClicked:(UIButton *)button{
   
//    int path = button.tag-10000;
    NSString *str = _bigArr[button.tag-10000][@"id"];
    NSLog(@"%@",str);
    [self quxiaodingdanHttp:[NSString stringWithFormat:@"%@",str]];
}
-(void)zhifuButtonClcike:(UIButton *)button{
    NSLog(@"%@",button.titleLabel.text);
    if ([button.titleLabel.text isEqualToString:@"申请售后"]) {
        
    }else if([button.titleLabel.text isEqualToString:@"取消订单"]){
        
        NSString *str = _bigArr[button.tag-10000][@"id"];
        
        [self quxiaodingdanHttp:[NSString stringWithFormat:@"%@",str]];
    }else if ([button.titleLabel.text isEqualToString:@"已取消"]){
        
    }

}
#pragma mark---网络请求
-(void)httpURLRquest{
    __weak typeof(self) waekself = self;
    [HTTPURL postRequest:LINK_BASE_URL(_urlString) parameters:@{@"userId":@(92),@"page":@(page)} success:^(NSURLSessionDataTask *task, id responseObject) {
        NSLog(@"%@",responseObject);
        if ([responseObject[@"retFlag"]intValue]==1) {
            NSDictionary *dic = [PublicTools dictionaryWithJsonString:responseObject[@"data"]];
            _bigArr = dic[@"rows"];
            [waekself addShoppingMallTableView];
        }
    } filure:^(NSURLSessionDataTask *task, id error) {
        NSLog(@"%@",error);
    }];
}
#pragma mark--取消订单
-(void)quxiaodingdanHttp:(NSString *)string{
     __weak typeof(self) waekself = self;
    [HTTPURL postRequest:LINK_BASE_URL(@"selfOrder/cancelOrderById.api") parameters:@{@"userId":@(92),@"orderId":string} success:^(NSURLSessionDataTask *task, id responseObject) {
        NSLog(@"%@",responseObject);
        if ([responseObject[@"retFlag"]intValue]==1) {
            [waekself httpURLRquest];
        }
    } filure:^(NSURLSessionDataTask *task, id error) {
        NSLog(@"%@",error);
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
